package project02.controller.message;

import java.util.List;
import java.util.logging.Logger;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import project02.dao.MessageDataDao;
import project02.model.MessageData;
import project02.model.UserInfo;

public class DeleteController extends Controller {

    @SuppressWarnings("unused")
    private static final Logger logger = Logger.getLogger(DeleteController.class.getName());

    @Override
    public Navigation run() {

        MessageDataDao dao = new MessageDataDao();
        UserInfo user = sessionScope("userInfo");

        if (!user.getPassword().equals(asString("password"))) {
            requestScope("result", "<script type=\"text/javascript\"> window.alert('パスワードが一致しません');</script>");
            List<MessageData> list = dao.findAllByUser(user.getUserId());
            requestScope("resultList", list);
            return forward("index.jsp");
        }

        MessageData entity = new MessageData();
        entity = dao.find(asString("dataId"));
        dao.deletePersistentInTx(entity);

        requestScope("result", "<script type=\"text/javascript\"> window.alert('メッセージを削除しました');</script>");

        List<MessageData> list = dao.findAllByUser(user.getUserId());

        requestScope("resultList", list);
        return forward("index.jsp");
    }
}
